﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Drawing;
using LevelEditor.Classes;
using System.Windows.Forms;

namespace LevelEditor
{
   public class XmlHandler
    {
        private System.Xml.XmlTextReader xmlReader;
        private System.Xml.XmlTextWriter xmlWriter;
        public String amountofPlayersOnMap;
        private String property = "";
        private String objectname = "";
        public LevelEditor editorForm;
        public String filename;
        public bool newLevel = false;
        public String currentDirectory;
        public XmlHandler(LevelEditor editorForm)
        {
            this.editorForm = editorForm;
        }
        public void save(String fileLoc)
        {
            if (!newLevel)
                xmlWriter = new XmlTextWriter(filename, null);
            else
                xmlWriter = new XmlTextWriter(fileLoc, null);

            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("level");
            xmlWriter.WriteStartElement("id");
            xmlWriter.WriteValue(editorForm.currentLevel);
            //end of level
            xmlWriter.WriteEndElement();

            xmlWriter.WriteStartElement("amountPlayers");
            xmlWriter.WriteValue(amountofPlayersOnMap);
            //end of level
            xmlWriter.WriteEndElement();

            xmlWriter.WriteStartElement("objects");
            foreach (CustomObject cu in editorForm.objectsOnScreen)
            {
                xmlWriter.WriteStartElement("object");
                xmlWriter.WriteStartElement("mass");
                xmlWriter.WriteValue(cu.mass);
                xmlWriter.WriteEndElement();
                
                xmlWriter.WriteStartElement("size");
                String size = cu.size.X + ":" + cu.size.Y;
                xmlWriter.WriteValue(size);
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("bodytype");
                xmlWriter.WriteValue(cu.bodyType);
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("position");
                String position = cu.Position.X + ":" + cu.Position.Y;
                xmlWriter.WriteValue(position);
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("rotation");
                xmlWriter.WriteValue(cu.rotation);
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("imglocation");
                xmlWriter.WriteValue(cu.relativeImageUrl);
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("name");
                xmlWriter.WriteValue(cu.name);
                xmlWriter.WriteEndElement();
                //end of object
                xmlWriter.WriteEndElement();
            }
            //end of objects
            xmlWriter.WriteEndElement();
            //end of level
            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
            //close
            xmlWriter.Close();
        }
        public void saveNew()
        {

        }
        /// <summary>
        /// Function to loop through the xml file and read the objects from it.
        /// How it works : Firstly you create local variables with the values that will be filled.
        /// After that you check what kind of node it is, is its a name node, the name will be
        /// filled in as property, after if it finds a 'text' node, it
        /// checks the property node and fills in the proper property.
        /// if its finds an end node it checks the objectname(which is filled in last)
        /// and then it adds the object to the manager.
        /// </summary>
        public void load(String filename)
        {
            xmlReader = new System.Xml.XmlTextReader(filename);
            //temp
            CustomObject gameObject = new CustomObject();
            String[] size = new String[2], position = new String[2];

            while (xmlReader.Read())
            {
                switch (xmlReader.NodeType)
                {
                    case XmlNodeType.Element: //Display element
                        property = xmlReader.Name;
                        break;
                    case XmlNodeType.Text: //Display the text in each element.
                        switch (property)
                        {
                            case "amountPlayers": amountofPlayersOnMap = xmlReader.Value;
                                editorForm.amountofPlayersOnMapText.Text = "Amount of players in this level " + amountofPlayersOnMap;
                                break;
                            case "name": gameObject.name = xmlReader.Value; objectname = xmlReader.Value; break;
                            case "mass": gameObject.mass = (float)Convert.ToDouble(xmlReader.Value); break;
                            case "size":
                                size = xmlReader.Value.ToString().Split(':');
                                gameObject.size.X = (float)Convert.ToDouble(size[0]);
                                gameObject.size.Y = (float)Convert.ToDouble(size[1]);
                                break;
                            case "position":
                                position = xmlReader.Value.ToString().Split(':');
                                gameObject.Position.X = (float)Convert.ToDouble(position[0]);
                                gameObject.Position.Y = (float)Convert.ToDouble(position[1]);
                                break;
                            case "imglocation":
                                gameObject.relativeImageUrl = xmlReader.Value.ToString();
                                gameObject.imageURL = (Application.StartupPath + "\\Classes\\" + xmlReader.Value.ToString());

                                gameObject.image = new Bitmap(gameObject.imageURL);
                                Console.WriteLine("");
                                break;
                            case "bodytype":
                                switch (xmlReader.Value)
                                {
                                    case "Kinematic": gameObject.bodyType = "Kinematic"; break;
                                    case "Static": gameObject.bodyType = "Static"; break;
                                    case "Dynamic": gameObject.bodyType = "Dynamic"; break;
                                }
                                break;
                        }
                        break;
                    case XmlNodeType.EndElement: //Display the end of the element.
                        if (!objectname.Equals("") && !gameObject.name.Equals(""))
                        {
                            objectname = "";
                            property = "";
                            editorForm.objectsOnScreen.Add(gameObject);
                            //editorForm.objects.Add(gameObject);
                            gameObject = new CustomObject();
                            Console.WriteLine("[XMLHandler] loaded object");
                            editorForm.pictureBox1.Invalidate();
                        }
                        break;
                }
            }
            xmlReader.Close();
        }
    }
}
